package com.zyb.patient.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;

import com.zyb.patient.activity.BaseActivity;
import com.zyb.patient.activity.LoadingActivity;

public class MainService extends Service implements Runnable {

	public static List<Task> allTask = new ArrayList<Task>();
	public static ArrayList<Activity> allActivity = new ArrayList<Activity>();
	public boolean isrun = false;
	Thread t = null;

	public static void newTask(Task task) {
		allTask.add(task);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		isrun = true;
		t = new Thread(this);
		t.start();
	}

	@Override
	public void onRebind(Intent intent) {
		t.start();
	}

	@SuppressWarnings("deprecation")
	public boolean onUnbind(Intent intent) {
		t.stop();
		return false;
	}

	@Override
	public void run() {
		while (isrun) {
			try {
				Task lastTask = null;
				synchronized (allTask) {
					if (allTask.size() > 0) {
						lastTask = allTask.get(0);
						doTask(lastTask);
					}
				}
				try {
					Thread.sleep(100);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	private Handler hand = new Handler(Looper.getMainLooper()) {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Task.CANCEL_LESSON: {
				BaseActivity ia = (BaseActivity) MainService
						.getActivityByName("AboutMeClassActivity");
				ia.refresh(Task.CANCEL_LESSON, msg.obj);
			}
				break;
			case Task.SEND_FEED_BACK: {
				BaseActivity ia = (BaseActivity) MainService
						.getActivityByName("SettingFeedBackActivity");
				ia.refresh(Task.SEND_FEED_BACK, msg.obj);
			}
				break;
			case Task.EDIT_USER_NAME: {
				BaseActivity ia = (BaseActivity) MainService
						.getActivityByName("UserForUpdateInfoActivity");
				ia.refresh(Task.EDIT_USER_NAME, msg.obj);
			}
				break;
				
			case Task.EDIT_USER_SEX: {
				BaseActivity ia = (BaseActivity) MainService
						.getActivityByName("AboutMeUserCenterActivity");
				ia.refresh(Task.EDIT_USER_SEX, msg.obj);
			}
				break;
				
			case Task.EDIT_USER_CITY: {
				BaseActivity ia = (BaseActivity) MainService
						.getActivityByName("AboutMeUserCenterActivity");
				ia.refresh(Task.EDIT_USER_CITY, msg.obj);
			}
				break;
				

			case Task.GET_INIT_DATA: {
				BaseActivity ia = (BaseActivity) MainService
						.getActivityByName("LoadingActivity");
				ia.refresh(Task.GET_INIT_DATA, msg.obj);
			}
				break;

			case Task.LOGIN_USER: {
				BaseActivity ia = (BaseActivity) MainService
						.getActivityByName("UserLoginActivity");
				ia.refresh(Task.LOGIN_USER, msg.obj);
			}
				break;

			case Task.GET_CODE: {
				BaseActivity ia = (BaseActivity) MainService
						.getActivityByName("UserLoginActivity");
				ia.refresh(Task.GET_CODE, msg.obj);
			}
				break;

			case Task.GET_VERSION: {
				BaseActivity ia = (BaseActivity) MainService
						.getActivityByName("Left1UserCenterActivity");
				ia.refresh(Task.GET_VERSION, msg.obj);
			}
				break;

			case Task.SEND_PUSH_USERID:
				break;

			case Task.GET_VERSION2: {
				LoadingActivity ia = (LoadingActivity) MainService
						.getActivityByName("LoadingActivity");
				ia.refresh(Task.GET_VERSION2, msg.obj);
			}
				break;

			default:
				break;
			}
		}
	};

	public void doTask(Task task) {
		Message mess = hand.obtainMessage();
		mess.what = task.getTaskID();
		try {
			switch (task.getTaskID()) {

			case Task.CANCEL_LESSON:
				mess.obj = DataService.cancelLesson(task);
				break;

			case Task.SEND_FEED_BACK:
				mess.obj = DataService.sendFeedback(task);
				break;

			case Task.EDIT_USER_NAME:
				mess.obj = DataService.edit_userinfo(task);
				
				break;
			case Task.EDIT_USER_SEX:
				mess.obj = DataService.edit_userinfo(task);
				break;
				
			case Task.EDIT_USER_CITY:
				mess.obj = DataService.edit_userinfo(task);
				break;

			case Task.GET_INIT_DATA:
				mess.obj = DataService.getAPPInfo(task);
				break;

			case Task.LOGIN_USER:
				mess.obj = DataService.login(task);
				break;

			case Task.GET_CODE:
				mess.obj = DataService.getCode(task);
				break;

			case Task.GET_VERSION:
				break;

			case Task.GET_VERSION2:
				break;

			case Task.SEND_PUSH_USERID:
				break;

			default:
				break;
			}
			hand.sendMessage(mess);
			allTask.remove(task);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Activity getActivityByName(String name) {
		for (Activity ac : allActivity) {
			if (ac.getClass().getName().indexOf("." + name) >= 0) {
				return ac;
			}
		}
		return null;
	}

	public static synchronized void addActivity(Activity activity) {
		String name = activity.getClass().getName();
		Iterator<Activity> iterator = allActivity.iterator();
		int num = 0;
		while (iterator.hasNext()) {
			Activity ac = (Activity) iterator.next();
			if (ac.getClass().getName().indexOf(name) >= 0) {
				allActivity.remove(num);
				break;
			}
			num++;
		}
		allActivity.add(activity);
	}
}
